package org.batatasframework.web;


public class HtmlRenderHelper {
	/** 双引号'"' */
	public static final String quot = "&quot;";
	/** 单引号"'" */
	public static final String acute = "&acute;";
	/** 单引号"'"(IE不支持) */
	public static final String apos = "&apos; ";
	/** ©copyright版权 */
	public static final String copyright = "&copy;";
	/** registered 注册trademark */
	public static final String registered = "&reg;";
	/** ¥ 元 */
	public static final String yen = "&yen;";
	/** multiplication乘 */
	public static final String multiplication = "&times;";
	/** “&” 号 */
	public static final String amp = "&amp;";
	/** 非换行空格 */
	public static final String blank = "&nbsp;";
	/** 半个空白位 */
	public static final String ensp = "&ensp;";
	/** 一个空白位 */
	public static final String emsp = "&emsp;";
	
	public static final String ATTRIBUTE_QUOT = "\"";
	
	private HtmlRenderHelper(){};
	
	private static final HtmlRenderHelper HTML_RENDER_HELPER = new HtmlRenderHelper();
	
	public static HtmlRenderHelper getHtmlRenderHelper(){
		return HTML_RENDER_HELPER;
	}
	
	public Object replace(Object value){
		if(value instanceof String){
			return ((String)value).replaceAll(ATTRIBUTE_QUOT, quot);
		}
		
		return value;
	}
	
	public static StringBuilder attribute(StringBuilder html, String attrName, int attrValue, boolean needAttr){
		return html.append(' ').append(attrName).append('=')
				.append(ATTRIBUTE_QUOT).append(attrValue).append(ATTRIBUTE_QUOT);
	}
	
	public static StringBuilder attribute(StringBuilder html, String attrName, Object attrValue, boolean needAttr){
		if(null != attrValue){
			html.append(' ').append(attrName).append('=')
					.append(ATTRIBUTE_QUOT).append(attrValue).append(ATTRIBUTE_QUOT);
		}else if(needAttr){
			html.append(' ').append(attrName).append('=').append(ATTRIBUTE_QUOT).append(ATTRIBUTE_QUOT);
		}
		return html;
	}
//	
//	public static String input(String type, HtmlAttribute attr, Object value){
//		return _input(type, attr, HTML_RENDER_HELPER.replace(value));
//	}
//	
//	public static String input(String type, HtmlAttribute attr, TemplateRender templateRender, String propretyName){
//		return _input(type, attr, templateRender.valueEscapeCharacter(propretyName));
//	}
//	
//	public static String textarea(HtmlAttribute attr, Object value){
//		StringBuilder html = new StringBuilder("<textarea");
//		attr.toHtml(html);
//		html.append('>');
//		HtmlRenderHelper.attribute(html, "value", value, true);
//		html.append("</textarea>");
//		return html.toString();
//	}
//	
//	public static String textarea(HtmlAttribute attr, TemplateRender templateRender, String propretyName){
//		return textarea(attr, templateRender.valueTemplate(propretyName));
//	}
//	
//	private static String _input(String type, HtmlAttribute attr, Object value){
//		StringBuilder html = new StringBuilder("<input");
//		HtmlRenderHelper.attribute(html, "type", type, false);
//		attr.toHtml(html);
//		/* 如果是字段串，则需要处理字符串里是否有"'"引号的问题。 */
//		HtmlRenderHelper.attribute(html, "value", value, true);
//		html.append("/>");
//		return html.toString();
//	}
}
